@extends('layouts.vote.unregister.vote')
@section('titleRules')
    @include('app.vote.unregister.titleRules')
@endsection
@section('sortcure','cur')
@section('contents')
    <!-- 排行  S-->
    <div class="pb30">
        <ul class="ui-list ui-list-link ui-list-sort">
        </ul>
    </div>
@endsection
@section('script')
    <script src="/js/vote/unregister/list.js"></script>
    <script type="text/javascript">
		var voteList = '<li >' +
			'<div class="ui-list-img" style="background-image:url(\'?src?\');">' +
			'<i class="?crown?"></i>' +
			'</div>' +
			'<div class="ui-list-info">' +
			'<h4 class="ui-nowrap">?title?</h4>' +
			'<p class="ui-nowrap-multi">?content?</p>' +
			'<div class="state">' +
			'<span class="ranking">排名：?rank?</span>' +
			'<span class="votes">?votes?票</span>' +
			'</div>' +
			'</div>' +
			'</li>';
		$(function () {
			var voteItems;
			var scores;

			function isEmpty(v) {
				return (
					(Array.isArray(v) && v.length == 0) || (Object.prototype.isPrototypeOf(v) && Object.keys(v).length == 0)
				);
			}

			function analyseRankList() {
				if (!isEmpty(voteItems)) {
					$('ul.ui-list-sort').empty()
					var rank = 0;
					for (var index in voteItems) {
						rank++;
						var crown = '';
						if (rank == 1) {
							crown = 'jin';
						} else if (rank == 2) {
							crown = 'yin';
						} else if (rank == 3) {
							crown = 'tong';
						}
						var listTemp = '';
						listTemp = voteList.replace(/\?crown\?/, crown);
						listTemp = listTemp.replace(/\?src\?/, voteItems[index]['cover']);
						listTemp = listTemp.replace(/\?title\?/, voteItems[index]['name']);
						listTemp = listTemp.replace(/\?content\?/, voteItems[index]['content']);
						listTemp = listTemp.replace(/\?rank\?/, rank.toString());
						listTemp = listTemp.replace(/\?votes\?/, scores[index]);
						$('ul.ui-list-sort').append(listTemp);
					}
				}
			}

			$.ajax({
				url: '{{env('LUA_SERVER')}}/api/vote/rank',
				data: 'vote_id={{$vote_id}}',
				type: 'get',
				dataType: 'json',
				success: function (response) {
					if (response.code == 200) {
						voteItems = response.items;
						scores = response.scores;
						analyseRankList();
					}
				},
				error: function () {

				}
			});
		});
    </script>
@endsection
